package com.svtar.ywg.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.svtar.ywg.R;
import com.svtar.ywg.ui.base.BaseActivity;
import com.svtar.ywg.ui.fragment.ConsoleFragment;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    /**
     * 当前显示的Fragment
     */
    private String curFragmentTag;
    /**
     * 导航条
     */
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_console)
    TextView tvConsole;
    /**
     * 抽屉布局
     */
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    /**
     * 用来管理Fragment
     */
    private FragmentManager fragmentManager;
    /**
     * 抽屉开关
     */
    private ActionBarDrawerToggle mDrawerToggle;
    /**
     * 控制台
     */
    public ConsoleFragment mConsoleFragment;

    /**
     * 首页所有子Fragment的枚举变量
     */
    public enum UiContent {
        CONSOLE
    }

    /**
     * 菜单开关(用来判断是否需要显示菜单)
     */
    private boolean menuSwitch = true;

    @Override
    protected void init(Bundle savedInstanceState) {
        //设置toolbar
        setSupportActionBar(mToolbar);
        //设置toolbar标题(默认为AppName)
            getSupportActionBar().setTitle(getString(R.string.console));
        //设置抽屉菜单按钮
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //创建返回键，并实现打开关/闭监听
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.menu, R.string.back) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        //默认加载控制台
        fragmentManager = getSupportFragmentManager();
        switchContent(UiContent.CONSOLE);
    }

    @Override
    protected void onBeforeSetContentLayout() {
        setContentView(R.layout.activity_main);
    }

    /**
     * 切换内容，传入的ID加载相应Fragment界面
     */
    public void switchContent(UiContent uiContent) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，防止有多个Fragment显示在界面上的情况。
        hideFragments(transaction);
        tvConsole.setBackgroundColor(getResources().getColor(R.color.white));
        switch (uiContent) {
            case CONSOLE://控制台
                //启用menu
                menuSwitch = false;
                //调用该函数会重新加载menu创建的方法
                supportInvalidateOptionsMenu();
                tvConsole.setBackgroundColor(getResources().getColor(R.color.gray_e2));
                curFragmentTag = ConsoleFragment.class.getSimpleName();
                    getSupportActionBar().setTitle(getString(R.string.console));
                //判断Fragment是否实例化
                if (mConsoleFragment == null) {
                    mConsoleFragment = new ConsoleFragment();
                    //添加Fragment到布局中，并且设置一个旗帜用来记录
                    transaction.add(R.id.fragment_container, mConsoleFragment, curFragmentTag);
                } else {
                    transaction.show(mConsoleFragment);
                }
                break;
        }
        //关闭菜单
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerLayout.closeDrawers();
        }
        transaction.commit();
    }

    @OnClick({R.id.tv_console})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_console://控制台
                switchContent(UiContent.CONSOLE);
                break;
        }
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (mConsoleFragment != null) {
            transaction.hide(mConsoleFragment);
        }
    }

    //back键不销毁活动
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
